package ink.teamwork.user.service.impl;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import ink.teamwork.user.dao.UserDAO;
import ink.teamwork.user.entity.User;
import ink.teamwork.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    @Override
    public boolean save(User user) {
        return userDAO.save(user);
    }

    @Override
    public boolean update(User user) {
        try {
            userDAO.save(user);
            return true;
        }catch (Exception e){
            return false;
        }
    }

    @Override
    public User queryByMobile(String mobile) {
        Search search = new Search();
        search.addFilterEqual("mobile", mobile);
        return userDAO.searchUnique(search);
    }

    @Override
    public User queryById(String id) {
        return userDAO.find(id);
    }

    @Override
    public SearchResult<User> queryPage(int start, int size) {
        Search search = new Search();
        search.setFirstResult(start);
        search.setMaxResults(size);
        return userDAO.searchAndCount(search);
    }
}
